<template>
	<view class='page_bd'>
		<view class="weui-tab">
			<view class='room-box dp-flex-arround' :data-address="item.chat_room_address" :data-id="item.roomid" :data-name="item.name" :data-tid="item.tid" v-for="(item,index) in chatList" :key="index"  :data-viewid="item.id"  @tap="roomdtlTap">
				<view class='left'>
					<view class="room-title">{{item.name}}</view>
					<image class='room-img' :src="item.chat" role='img'></image>
				</view>
				<view class="right">
					<image class="right-imgs" :src="item.cover" role="img"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {GYZJMODEL as gyzjModel}from '@/utils/gyzjModel.js';
	import app from '@/App.vue';
	var NIM = require('@/vendor/NIM_Web_NIM_weixin_v5.9.1.js');
	export default {
		data() {
			return {
				// 群组
				chatList: '',
				roomName: '',
				viewId: '',
				teamId: '',
				city: "",
			}
		},
		onLoad:function(){
			let globalData=app.globalData;
			console.log('我是globalData',globalData);
			console.log('我是mobile',globalData.mobile);
			console.log('我是userInfo',globalData.userInfo);
			console.log('我是chatroomServer',globalData.chatroomServer);
			// 群组
			var token = 'Q8dNXgEwIn7dDrOw7GPCXQ==';
			// let appKey = "7d169e7365827a4e517f19bc1f9d4d3e";
			let appKey = "c366812544b1d978c223249f7c22f911";
			console.log('gyzjModel',gyzjModel);
			let prevtoken = globalData.prevtoken;
			let account=globalData.userInfo.accid;
			// var timer=setInterval(res=>{
			// 	if(globalData.userInfo){
			// 		let account=globalData.userInfo.accid;
			// 		console.log('account的值',account);
			// 		clearInterval(timer);
			// 	}
			// })
			gyzjModel.prototype.getChatList(214).then(res=>{
				let normal=res.data.data;
				gyzjModel.prototype.getSeniorList('214').then(res=>{
					let news = res.data.data.concat(normal)//合并群组
					this.chatList=news;
				})
			})
			console.log('所有的值开始');
			console.log('appKey', appKey);
			console.log('account', account);
			console.log('token', prevtoken);
			console.log('onconnect', this.onConnect);
			console.log('onerror', this.onError);
			console.log('onwillreconnect', this.onWillReconnect);
			console.log('ondisconnect', this.onDisconnect);
			console.log('onsyncdone', this.onSyncDone);
			console.log('onmsg', this.onMsg);
			/*网易云的接入开始*/
			let self = this;
			var nim=NIM.getInstance({
				//初始化SDK
				appKey:appKey,
				account:account,
				token:prevtoken,
				onconnect:self.onConnect,
				onerror:self.onError,
				onwillreconnect:self.onWillReconnect,
				ondisconnect:self.onDisconnect,
				//同步完成
				onsyncdone:self.onSyncDone,
				//聊天消息
				onmsg:self.onMsg
			})
			console.log('nim的值',nim);
			//暂存群实例
			globalData.nim=nim;
			// gyzjModel._show_error('我的天');
			/*聊天室开始*/
			// uni.request({//聊天室
			// 	url: 'https://m.zxgyzj.com/room/list/'+214,
			// 	method: 'GET',
			// 	data: {},
			// 	success: res => {
			// 		var normal=res.data.data.data;
			// 		console.log('normal',normal);
			// 		uni.request({ //高级群
			// 			url: 'https://m.zxgyzj.com/group/list/'+214+"&token="+token,
			// 			method: 'GET',
			// 			data: {},
			// 			success: res => {
			// 				let news=res.data.data.data;
			// 				normal.unshift(news[0]);
			// 				this.chatList=normal;
			// 				uni.hideLoading({
			// 					title:'加载中....'
			// 				})
			// 			},
			// 			fail: () => {},
			// 			complete: () => {}
			// 		});
			// 	},
			// 	fail: () => {},
			// 	complete: () => {}
			// });
			// uni.login({
			// 	provider:'weixin',
			// 	success:function(loginRes){
			// 		console.log('loginRes',loginRes);
			// 		uni.showToast({
			// 			title:JSON.stringify(loginRes)
			// 		});
			// 		console.log(loginRes.authResult);
			// 	}
			// })
		},
		methods: {
		  //点击进群
		  roomdtlTap: function (e) {
			var userInfo = app.globalData.userInfo;
			if (userInfo.type == 1) {
			  return uni.showToast({
				title: '您还未入驻,请先入驻！',
				icon: 'none'
			  });
			}
			console.log(e)
			let roomId = e.currentTarget.dataset.id
			let roomAddress = e.currentTarget.dataset.address;
			let roomName = e.currentTarget.dataset.name;
			let viewId = e.currentTarget.dataset.viewid;
			let city = this.city;
			let teamId = e.currentTarget.dataset.tid;//高级群id
			console.log('所有的值roomId',roomId);
			console.log('所有的值roomAddress',roomAddress);
			console.log('所有的值roomName',roomName);
			console.log('所有的值viewId',viewId);
			console.log('所有的值city',city);
			console.log('所有的值teamId',teamId);
			
			if (e.currentTarget.dataset.tid) {
			  this.teamId= teamId;
			  this.roomName=roomName;
			  this.viewId=viewId;
			  app.globalData.nim.applyTeam({
				teamId: teamId,
				ps: '申请入群',
				done: (error, obj) => {
				  if (error) {
					console.log(error, 'a失败')
					uni.navigateTo({
					  url: './chat-room/senior-chat/senior-chat?teamId=' + teamId + '&teamName=' + roomName + '&id=' + viewId + "&city=" + city,
					});
					return
				  } else {
					uni.showToast({
					  title: "申请成功，即将进入群聊",
					  icon: 'none',
					  duration: 2000,
					})
					setTimeout(function () {
					  uni.navigateTo({
						url: './chat-room/senior-chat/senior-chat?teamId=' + teamId + '&teamName=' + roomName + '&id=' + viewId + "&city=" + city,
					  });
					}, 2000)
				  }
				}
			  })
			} else {
			  uni.navigateTo({
				url: './chat-room/chat-room-detail/chat-room-detail?roomId=' + roomId + '&' + 'roomAddress=' + roomAddress + '&' + 'roomName=' + roomName + '&' + 'id=' + viewId,
			  });
			}
		  }

		}	
	}
</script>

<style>
	@import url("../../static/style/app.css");
.weui-tab {
	position: relative;
	height: 100%;
}
.room-box {
	width: 95%;
	height: 300rpx;
	border-radius: 10rpx;
	margin-left: 2.5%;
	margin-top: 20rpx;
	background: white;
	align-items: center;
}
.dp-flex-arround {
	display: flex;
	justify-content: space-around;
}
.left {
	width: 35%;
	height: 85%;
	margin-left: 20rpx;
}
.room-title {
	width: 100%;
	height: 20%;
	font-size: 30rpx;
	font-weight: 800;
	margin-left: 10rpx;
	text-overflow: ellipsis;
}
.room-img {
	width: 85%;
	height: 80%;
	margin-left: 10rpx;
}
.right {
	width: 63%;
	height: 85%;
	margin-right: 20rpx;
}
.right-imgs {
	width: 100%;
	height: 100%;
}
/*小程序page的代码*/
page{
	width: 100%;
	height: 100%;
	background-color: #efeff4;
	font-size: 16px;
	font-family: -apple-system-font, Helvetica Neue, Helvetica, sans-serif;
}

</style>
